package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.UncaughtErrorEvent;
    
    public class Application extends Sprite
    {
        public function Application()
        {
            super();
            _ctor();
        }
        
        private function _ctor():void
        {
            
            //wait for UI
            if( stage )
            {
                onAddedToStage();
            }
            else
            {
                addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
            }
        }
        
        
        //---- logic flow ----
        
        private function onStartup():void
        {
            main();
        }
        
        private function onReady():void
        {
            
        }
        
        
        //---- events ----
        
        private function onGlobalError( event:UncaughtErrorEvent = null ):void
        {
            var msg:String = "";
            
            if( event && event.error )
            {
                if( event.error.message )
                {
                    msg = event.error.message;
                }
                else if( event.error.text )
                {
                    msg = event.error.text;
                }
                else
                {
                    msg = event.error.toString();
                }
            }
            else
            {
                msg = "unknown";
            }
            
            msg = "## Global Error " + msg + " ##";
            
            trace( msg );
        }
        
        private function onAddedToStage( event:Event = null ):void
        {
            removeEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
            onStartup();
        }
        
        
        public function main():void
        {
            trace( "Application" );
        }
    }
}